package com.wty.member.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wty.commons.result.DataGrid;
import com.wty.commons.result.Result;
import com.wty.member.pojo.Member;
import com.wty.member.service.MemberService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wty
 * @since 2020-04-28
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /*注册会员*/
    @ApiOperation("注册会员")
    @ApiImplicitParam(name = "member", required = true, dataType = "Member", dataTypeClass = Member.class)
    @PostMapping
    public Result<Member> addMember(@RequestBody Member member) throws Exception {
        memberService.save(member);
        return Result.OK(member);
    }

    /*登陆*/
    @ApiOperation("会员登录")
    @ApiImplicitParam(name = "member", required = true, dataType = "Member", dataTypeClass = Member.class)
    @PostMapping("login")
    public Result<Member> login(@RequestBody Member member)throws Exception{
        Wrapper<Member> wrapper = new QueryWrapper<>(member);  // 构建一个查询的条件包装器
        Member memberData = memberService.getOne(wrapper);
        if (ObjectUtils.isEmpty(memberData)){
            // 登录失败
            return Result.CUSTOM_ERROR("登录失败");
        }else {
            // 登陆成功
            return Result.OK(memberData);
        }
    }


    /*修改会员资料*/
    @ApiOperation("修改会员基本信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", required = true, dataType = "Long", dataTypeClass = Long.class),
        @ApiImplicitParam(name = "member", required = true, dataType = "Member", dataTypeClass = Member.class)
    })
    @PutMapping("{id}")
    public Result<Member> updateMember(@PathVariable Long id,@RequestBody Member member)throws Exception{
        member.setMemberId(id);
        memberService.updateById(member);
        Member memberData = memberService.getById(id);
        return Result.OK(memberData);
    }

    /*分页查询会员信息*/
    @ApiOperation("分页查询会员信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "current", required = true, dataType = "Long", dataTypeClass = Long.class),
        @ApiImplicitParam(name = "size", required = true, dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping("selectList/{current}/{size}")
    public Result<DataGrid<Member>> selectList(@PathVariable Long current,@PathVariable Long size){
        DataGrid<Member> dataGrid = new DataGrid<>();
        IPage<Member> page = memberService.page(new Page<>(current, size));
        dataGrid.setTotal(page.getTotal());
        dataGrid.setRows(page.getRecords());
        return Result.OK(dataGrid);
    }

    /*分页条件查询会员信息*/
    @ApiOperation("分页条件查询会员信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "current", required = true, dataType = "Long", dataTypeClass = Long.class),
        @ApiImplicitParam(name = "size", required = true, dataType = "Long", dataTypeClass = Long.class),
        @ApiImplicitParam(name = "member", required = true, dataType = "Member", dataTypeClass = Member.class)
    })
    @PostMapping("selectListByPage/{current}/{size}") // 只有 PostMapping 和 PutMapping 才能携带 RequestBody
    public Result<DataGrid<Member>> selectListByPage(@PathVariable Long current,@PathVariable Long size,@RequestBody Member member){
        DataGrid<Member> dataGrid = new DataGrid<>();
        Wrapper<Member> queryWrapper = new QueryWrapper<>(member); // 查询条件
        IPage<Member> page = memberService.page(new Page<>(current, size),queryWrapper);
        dataGrid.setTotal(page.getTotal());
        dataGrid.setRows(page.getRecords());
        return Result.OK(dataGrid);
    }

}

